clc;
clear all;

shot=4417;

[Val1,Time1]=downloaddata(shot,'temp01','-2:7:1e-5',0,0);

a1=max(Val1);
HCN = Val1;
num=length(HCN);
time=Time1;

threshold1 =1.3;                  % noise margin of the signal
tag             = 8;             % full amplitude of the sine signal

o_density  = Val1 + 4.8;    % to make the signal all positive
l_density(1:num)=0;              % 


l_density=l_density';
l_density(1)=o_density(1);

    
    for i=1:length(HCN)-1
        tFlag=1;
        
        if abs(o_density(i+1)-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1);tFlag=0;
        end
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        if abs(o_density(i+1)-tag*1-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1) - tag*1; tFlag=0; % make sure here the judgement statement is 'minus'
        end
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
       
        if abs(o_density(i+1)+tag*1-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*1; tFlag=0;  % the first time turn over
        end
        if abs(o_density(i+1)+tag*2-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*2; tFlag=0;  % the second time turn over
        end
        if abs(o_density(i+1)+tag*3-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*3;tFlag=0;   %the third time turn over
        end
        if abs(o_density(i+1)+tag*4-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*4; tFlag=0;  %the fourth time turn over
        end
        if abs(o_density(i+1)+tag*5-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*5; tFlag=0;  %the fifth time turn over
        end
        if abs(o_density(i+1)+tag*6-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*6;tFlag=0;   %the sixth time turn over
        end
        if abs(o_density(i+1)+tag*7-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*7; tFlag=0;  %the seventh time turn over
        end
        if abs(o_density(i+1)+tag*8-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*8; tFlag=0;  %the eighth time turn over
        end
        if abs(o_density(i+1)+tag*9-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*9;tFlag=0;   %the ninth time turn over
        end
        if abs(o_density(i+1)+tag*10-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*10; tFlag=0; %the tenth time turn over
        end
        if abs(o_density(i+1)+tag*11-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*11; tFlag=0;
        end
        if abs(o_density(i+1)+tag*12-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*12; tFlag=0;
        end
        if abs(o_density(i+1)+tag*13-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*13; tFlag=0;
        end
        if abs(o_density(i+1)+tag*14-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*14; tFlag=0;
        end
        if abs(o_density(i+1)+tag*15-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)+tag*15; tFlag=0;
        end
        if abs(o_density(i+1)-tag*6-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)-tag*6; tFlag=0;
        end
        if abs(o_density(i+1)-tag*5-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)-tag*5; tFlag=0;
        end
        if abs(o_density(i+1)-tag*4-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)-tag*4; tFlag=0;
        end
        if abs(o_density(i+1)-tag*3-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)-tag*3; tFlag=0;
        end
        if abs(o_density(i+1)-tag*2-l_density(i))<=threshold1
            l_density(i+1)=o_density(i+1)-tag*2; tFlag=0;
        end
        
        if (i>20)&&(tFlag==1)
            s_density=0;
            for n=i:-1:(i-19)
                s_density=s_density+l_density(n);
            end
            a_density=s_density/20;
            l_density(i+1)=a_density;
        end
    end
    
    dl = mean(l_density(1:1000));
s_density=0;
%for n=270001:271000
% for n=999001:1000000
%   s_density=s_density+l_density(n);
% end
% dl=s_density/1000;
dl=0;
%n_INT = (l_density-dl);
n_INT = (l_density-dl)/4*pi*35.5/3.37/2;
    nel = n_INT;


figure
plot(Time1,nel);

